<?php

namespace app\common\model;

//use think\Model;

class OutConfigModel extends BaseModel{

	protected $table = 'tp_out_config';

	/*
	提币列表
	 */
	public function outList($uid){
		$data = $this->alias('e')
						->join('tp_coin_wallet w','w.id=e.wid and w.status=1')
						->join('tp_user_balance b','b.wid=e.wid and b.uid='.$uid,'LEFT')
						->group('e.wid')
						->order('e.sort asc')
						->field('e.wid,w.icon,w.name,w.price,b.balance')
						->select();
		if(empty($data)){
			return [];
		}
		foreach ($data as $key => $val) {
			$data[$key]['balance'] = $data[$key]['balance']?:0;
			$data[$key]['balance'] = $data[$key]['balance']/1;

            //获取钱包下面的币种列表
            $data[$key]['config_list'] = $this->alias('e')
            				->join('tp_coin c','c.id=e.cid and c.status=1')
            				->join('tp_coin_wallet w','w.id=e.fee_wid and w.status=1')
                            ->join('tp_user_wallet_address a','a.cid=c.id and uid='.$uid,'LEFT')
                            ->where('e.wid',$val['wid'])
                            ->order('e.sort asc')
                            ->field('e.*,c.name,a.address,w.name fee_name')
                            ->select();
		}
		return  $data;
	}

	/*
	获取单个钱包配置
	 */
	public function outOne($uid,$wid){
		$data = $this->alias('e')
						->join('tp_coin_wallet w','w.id=e.wid and w.status=1')
						->join('tp_user_balance b','b.wid=e.wid and b.uid='.$uid,'LEFT')
						->where('e.wid',$wid)
						->field('e.wid,w.icon,w.name,w.price,b.balance')
						->find();
		if(empty($data)){
			return [];
		}

		$data['balance'] = $data['balance']?:0;
		$data['balance'] = $data['balance']/1;

        //获取钱包下面的币种列表
        $data['config_list'] = $this->alias('e')
        				->join('tp_coin c','c.id=e.cid and c.status=1')
        				->join('tp_coin_wallet w','w.id=e.fee_wid and w.status=1')
                        ->join('tp_user_wallet_address a','a.cid=c.id and uid='.$uid,'LEFT')
                        ->where('e.wid',$data['wid'])
                        ->order('e.sort asc')
                        ->field('e.*,c.name,a.address,w.name fee_name')
                        ->select();
		return  $data;
	}


	/*
	获取一条配置
	 */
	public function getOneConfig($id,$uid){
		$data = $this->alias('e')
						->join('tp_coin_wallet w1','w1.id=e.wid and w1.status=1')
						->join('tp_coin c','c.id=e.cid and c.status=1')
						->join('tp_coin_wallet w3','w3.id=e.fee_wid and w3.status=1')
						->join('tp_user_balance b','b.wid=e.wid and b.uid='.$uid,'LEFT')
						->where('e.id',$id)	
						->field('e.*,b.balance,c.name coin_name,w1.name wallet_name')
						->find();
		if ($data) {
			$data['balance'] = $data['balance']?:0;
		}
		return $data;
	}
}